/*	$Id: datetest.c 983 2009-09-01 02:45:42Z phrakt $	*/
/*
 * Copyright (c) 2008 Jean-Francois Brousseau <phrakt@gmail.com>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/types.h>

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include <canopy/log.h>

#include <http/http.h>
#include <http/date.h>

#include "test.h"



struct date_desc {
	time_t  date_secs;
	char   *date_rfc1123;
	char   *date_rfc850;
	char   *date_ansic;
	char   *date_iso8601;
};




struct date_desc tests[] = {
	{
		0,
		"Thu, 01 Jan 1970 00:00:00 GMT",
		"Thursday, 01-Jan-70 00:00:00 GMT",
		"Thu Jan  1 00:00:00 1970",
		"1970-01-01T00:00:00Z",
	},
	{
		1567345,
		"Mon, 19 Jan 1970 03:22:25 GMT",
		"Monday, 19-Jan-70 03:22:25 GMT",
		"Mon Jan 19 03:22:25 1970",
		"1970-01-19T03:22:25Z",
	},
	{
		213567345,
		"Thu, 07 Oct 1976 20:15:45 GMT",
		"Thursday, 07-Oct-76 20:15:45 GMT",
		"Thu Oct  7 20:15:45 1976",
		"1976-10-07T20:15:45Z",
	},
};

int
test_parse_date(struct date_desc *desc, enum http_date_format fmt)
{
	int ret;
	time_t t;
	char buf[64], *val;

	if (fmt == HTTP_DATE_RFC1123)
		val = desc->date_rfc1123;
	else if (fmt == HTTP_DATE_RFC850)
		val = desc->date_rfc850;
	else if (fmt == HTTP_DATE_ANSIC)
		val = desc->date_ansic;
	else if (fmt == HTTP_DATE_ISO8601)
		val = desc->date_iso8601;

	ret = http_date_parse(val, fmt, &t);
	if (ret == -1)
		return (TEST_FAIL);

	TEST_ASSERT_TIMES_EQUAL("seconds since Epoch", desc->date_secs, t);

	return (TEST_OK);
}


int
test_format_date(struct date_desc *desc, enum http_date_format fmt)
{
	int ret;
	char buf[64], *val;

	ret = http_date_format(desc->date_secs, fmt, buf, sizeof(buf));
	if (ret == -1)
		return (TEST_FAIL);

	if (fmt == HTTP_DATE_RFC1123)
		val = desc->date_rfc1123;
	else if (fmt == HTTP_DATE_RFC850)
		val = desc->date_rfc850;
	else if (fmt == HTTP_DATE_ANSIC)
		val = desc->date_ansic;
	else if (fmt == HTTP_DATE_ISO8601)
		val = desc->date_iso8601;

	TEST_ASSERT_STRINGS_EQUAL("date", val, buf);

	return (TEST_OK);
}


int
main(int argc, char **argv)
{
	int result;
	unsigned int i;

	http_init();
	test_init();

	for (i = 0; i < (sizeof(tests) / sizeof(tests[0])); i++) {
		test_start("test_parse_date_rfc1123");
		result = test_parse_date(&tests[i], HTTP_DATE_RFC1123);
		test_end(result);
	}

	for (i = 0; i < (sizeof(tests) / sizeof(tests[0])); i++) {
		test_start("test_parse_date_rfc850");
		result = test_parse_date(&tests[i], HTTP_DATE_RFC850);
		test_end(result);
	}

	for (i = 0; i < (sizeof(tests) / sizeof(tests[0])); i++) {
		test_start("test_parse_date_ansic");
		result = test_parse_date(&tests[i], HTTP_DATE_ANSIC);
		test_end(result);
	}

	for (i = 0; i < (sizeof(tests) / sizeof(tests[0])); i++) {
		test_start("test_parse_date_iso8601");
		result = test_parse_date(&tests[i], HTTP_DATE_ISO8601);
		test_end(result);
	}


	for (i = 0; i < (sizeof(tests) / sizeof(tests[0])); i++) {
		test_start("test_format_date_rfc1123");
		result = test_format_date(&tests[i], HTTP_DATE_RFC1123);
		test_end(result);
	}

	for (i = 0; i < (sizeof(tests) / sizeof(tests[0])); i++) {
		test_start("test_format_date_rfc850");
		result = test_format_date(&tests[i], HTTP_DATE_RFC850);
		test_end(result);
	}

	for (i = 0; i < (sizeof(tests) / sizeof(tests[0])); i++) {
		test_start("test_format_date_ansic");
		result = test_format_date(&tests[i], HTTP_DATE_ANSIC);
		test_end(result);
	}

	for (i = 0; i < (sizeof(tests) / sizeof(tests[0])); i++) {
		test_start("test_format_date_iso8601");
		result = test_format_date(&tests[i], HTTP_DATE_ISO8601);
		test_end(result);
	}

	test_cleanup();

	return (0);
}
